/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simpleGame.client;

import com.jme3.app.SimpleApplication;
import com.jme3.input.controls.ActionListener;
import com.jme3.math.Vector3f;
import com.jme3.network.Client;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;
import com.jme3.network.Network;
import com.jme3.system.JmeContext;
import com.simsilica.es.EntityData;
import com.simsilica.es.EntitySet;
import com.simsilica.es.client.RemoteEntityData;
import com.simsilica.es.filter.FieldFilter;
import com.simsilica.es.net.EntitySerializers;
import de.tsajar.es.EntityDataAppState;
import de.tsajar.es.input.PlayerInputComponent;
import de.tsajar.es.position.PositionComponent;
import de.tsajar.es.render.RenderSystem;
import modelinitializer.CubeInitializer;
import networked.Helper;
import networked.ShootMessage;
import simpleGame.ConnectionAcceptedMessage;
import simpleGame.WalkDirectionMessage;

/**
 *
 * @author eike
 */
public class Main extends SimpleApplication implements ActionListener, MessageListener {

    EntityData ed;
    Client c;
    InputAppState ias;
    EntitySet es;
    int id = -1;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Main m = new Main();
        m.start();
//        m.start(JmeContext.Type.Headless);
    }
    private void initMyClient(String host, int port) {
        try {
            Helper.initSerializers();
            c = Network.connectToServer(host, port);
            c.addMessageListener(this);
            c.start();
            de.tsajar.es.networking.client.RemoteEntityData red = new de.tsajar.es.networking.client.RemoteEntityData(c);
            stateManager.attach(new EntityDataAppState(red));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private void initPaulsClient(String host, int port) {
        EntitySerializers.initialize();
        try {
            Helper.initSerializers();
            c = Network.connectToServer(host, port);
            c.addMessageListener(this);
            c.start();
            RemoteEntityData red = new RemoteEntityData(c, 0);
            stateManager.attach(new EntityDataAppState(red));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void simpleInitApp() {
        initMyClient("localhost", 1234);
//        initPaulsClient("localhost", 1235);
        ed = stateManager.getState(EntityDataAppState.class).getEd();
        stateManager.attach(new RenderSystem(new CubeInitializer(assetManager)));
        ias = new InputAppState();
        stateManager.attach(ias);
    }
    float tpu = 0;

    @Override
    public void simpleUpdate(float tpf) {

        if (es != null) {
            tpu += tpf;
            if (es.applyChanges()) {
                System.out.println(tpu);
                tpu = 0;
            }
            cam.setLocation(es.iterator().next().get(PositionComponent.class).getPosition());
            c.send(new WalkDirectionMessage(ias.getWalkDirection()));
        } else if (id != -1) {
            System.out.println(id);
            es = ed.getEntities(FieldFilter.create(PlayerInputComponent.class, "inputId", id), PlayerInputComponent.class, PositionComponent.class);
  es = ed.getEntities(PlayerInputComponent.class, PositionComponent.class);

        }
    }

    @Override
    public void onAction(String name, boolean isPressed, float tpf) {
        if (isPressed) {
            if (name.equals("SHOOT")) {
                fireArrow(cam.getLocation().clone(), cam.getDirection().normalize().clone());
            }
        }
    }

    private void fireArrow(Vector3f location, Vector3f direction) {
        ShootMessage sm = new ShootMessage(location, direction);
        sm.setReliable(true);
        c.send(sm);
    }

    public void messageReceived(Object source, Message m) {
        if (m instanceof ConnectionAcceptedMessage) {
            id = (int) ((ConnectionAcceptedMessage) m).entityId;

        }
    }
}
